package tt.dz.biz.dao.merch;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.ChangeDayVO;

@Repository("changeDayDAO")
public class ChangeDayDAOImp
  extends BaseDAO
{
  private static final String QUERY_MS_CHANGE_DAY = "select  change_code,ytd_accounting_date,tmr_accounting_date,accounting_date,updated_date  from ms_change_day ";
  private static final String UPDATE_MS_CHANGE_DAY = "update ms_change_day  set change_code=:changeCode,ytd_accounting_date=:ytdAccountingDate,tmr_accounting_date=:tmrAccountingDate,accounting_date=:accountingDate,updated_date=:updatedDate ";
  private static final String UPDATE_MERCHANT_SUBJECT_ACC = " update ms_merchant_subject_acc set last_turnover = (last_bal - acct_bal) ,  last_bal=acct_bal";
  private static final String SELECT_UPDATEDATE_CHANGE_DAY = "select updated_date from ms_change_day ";
  
  public ChangeDayVO queryChangeDay()
  {
    MapSqlParameterSource parameters = null;
    List<ChangeDayVO> tmpChangeVoList = queryForList("select  change_code,ytd_accounting_date,tmr_accounting_date,accounting_date,updated_date  from ms_change_day ", parameters, ChangeDayVO.class);
    if (tmpChangeVoList.size() > 0) {
      return (ChangeDayVO)tmpChangeVoList.get(0);
    }
    return null;
  }
  
  public Date queryAccountingDateForChangeDay()
  {
    ChangeDayVO changeDayVo = queryChangeDay();
    if (changeDayVo != null) {
      return changeDayVo.getAccountingDate();
    }
    return null;
  }
  
  public int updateChangeDay(ChangeDayVO changeDayVo)
  {
    return save("update ms_change_day  set change_code=:changeCode,ytd_accounting_date=:ytdAccountingDate,tmr_accounting_date=:tmrAccountingDate,accounting_date=:accountingDate,updated_date=:updatedDate ", changeDayVo);
  }
  
  public int updteSubjectAccLastBal()
  {
    MapSqlParameterSource parameters = null;
    return save(" update ms_merchant_subject_acc set last_turnover = (last_bal - acct_bal) ,  last_bal=acct_bal", parameters);
  }
  
  public Date getUpdateDateByChangeDay()
  {
    Map<String, ?> paramMap = null;
    try
    {
      return (Date)this.jdbcTemplate.queryForObject("select updated_date from ms_change_day ", paramMap, Date.class);
    }
    catch (EmptyResultDataAccessException emptye)
    {
      LogUtil.debug("查询不到任何记录select updated_date from ms_change_day ");
      return null;
    }
    catch (IncorrectResultSizeDataAccessException imcorrecte)
    {
      LogUtil.debug("查询的记录数大于1select updated_date from ms_change_day ");
    }
    return null;
  }
}
